<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleRePush({})" v-auth="'btn:reset:tstbz'">
          <Icon icon="ant-design:undo-outlined" /> {{ t('重置推送状态') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectname }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)">
          <span style="text-decoration: underline; margin-right: 5px">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
        
      </template>

      <template #uploadFile1="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'ProjectPushBz_file'"
          :uploadType="'all'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <Modal @register="registerModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjInfoPmProjectinformationList',
  });
</script>
<script lang="ts" setup>
  import { BasicUpload } from '/@/components/Upload';
  import { defineComponent, onMounted, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmProjectinformationDelete,
    pmProjectinformationListData,
    userCollectSave,
    userCollectCancel,
    userCollectListDataNoPage,
    pmProjectinformationRePush,
    pmProjectinformationBackPush,
  } from '/@/api/proj/info/pmProjectinformation';
  import { pmWaterListData, pmWaterSave } from '/@/api/process/water/pmWater';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import Modal from './modal.vue';

  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('proj.info.pmProjectinformation');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('通水推送报装管理'),
    value: t('通水推送报装管理'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('项目编号'),
        field: 'projectno',
        component: 'Input',
        // defaultValue: userStore.getProjecte.code
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('推送状态'),
        field: 'iswatersupply',
        component: 'Select',
        componentProps: {
          dictType: 'zf_push_bz_status',
          allowClear: true,
          onChange: handleSuccess,
          defaultValue: '0',
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('推送状态'),
      dataIndex: 'iswatersupply',
      key: 'a.iswatersupply',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'zf_push_bz_status',
      fixed: 'left',
    },
   
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'projectno',
    },

    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
  
    {
      title: t('推送时间'),
      dataIndex: 'watersupplytime',
      key: 'a.watersupplytime',
      sorter: true,
      width: 130,
      align: 'center',
      // fixed: 'left',
    },
    {
      title: t('是否零星'),
      dataIndex: 'bsmall',
      key: 'a.bsmall',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('推送异常信息'),
      dataIndex: 'errorcontent',
      key: 'a.errorcontent',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('推送附件'),
      dataIndex: 'assayupdate',
      key: 'a.assayupdate',
      sorter: true,
      width: 80,
      align: 'center',
      fixed: 'right',
      slot: 'uploadFile1',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 280,
    align: 'left',
    actions: (record: Recordable) => [
      

      {
        // icon: 'ant-design:undo-outlined',
        // color: 'warning',
        type: 'default',
        title: t('重置推送状态'),
        label: '重置推送状态',
        popConfirm: {
          title: t('是否确认重置推送状态'),
          confirm: handleRePush.bind(this, { id: record.id }),
        },
        auth: 'btn:reset:tstbz',
      },
      

      {
        // icon: 'ant-design:eye-filled',
        type: 'default',
        title: t('查看开户数'),
        label: '查看开户数',
        onClick: handleView.bind(this, record),
        auth: 'btn:view:tstbz',
      },
      {
        // icon: 'clarity:note-edit-line',
        type: 'default',
        title: t('通水推送报装'),
        label: t('通水推送报装'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'btn:send:tstbz',
        ifShow: () => record.iswatersupply != '1' && record.iswatersupply != '2',
      },
      {
        // icon: 'ant-design:undo-outlined',
        // color: 'warning',
        type: 'default',
        title: t('撤回通水 '),
        label: '撤回通水 ',
        popConfirm: {
          title: t('是否确认撤回通水'),
          confirm: handleBackPush.bind(this, { id: record.id }),
        },
        auth: 'proj:info:pmProjectinformation:back',
        ifShow: () => record.iswatersupply == '1' || record.iswatersupply == '2',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: pmWaterListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleView(record: Recordable) {
    openModal(true, record);
  }

  async function handleSave(record: Recordable) {
    let data = {
      projectCode: record.projectno,
      projectName: record.projectname,
    };
    const res = await userCollectSave(data);
    showMessage(res.message);
    handleSuccess();

    let res1 = await userCollectListDataNoPage();
    await userStore.setProjecteList(res1);
  }

  async function handleCancel(record: Recordable) {
    let data = {
      projectCode: record.projectno,
      // projectName:record.projectname
    };
    const res = await userCollectCancel(data);
    showMessage(res.message);
    handleSuccess();

    let res1 = await userCollectListDataNoPage();
    // console.log(res1);
    await userStore.setProjecteList(res1);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmProjectinformationDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  onMounted(async () => {
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: router.currentRoute.value.query.projectno
        ? router.currentRoute.value.query.projectno
        : props.projectno
        ? props.projectno
        : userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: router.currentRoute.value.query.projectno
        ? router.currentRoute.value.query.projectno
        : props.projectno
        ? props.projectno
        : userStore.getProjecte.code,
    });
  }

  // 重新推送
  // async function handleRePush() {
  //   if (getSelectRowKeys().length) {
  //     const res = await pmProjectinformationRePush({ selIds: getSelectRowKeys().join(',') });
  //     showMessage(res.message);
  //     handleSuccess();
  //   } else {
  //     showMessage('请先选择一行数据!');
  //   }
  // }
  async function handleRePush(record: Recordable) {
    const res = await pmProjectinformationRePush({ selIds: record.id });
    showMessage(res.message);
    handleSuccess();
  }

  async function handleBackPush(record: Recordable) {
    const res = await pmProjectinformationBackPush({ selIds: record.id });
    showMessage(res.message);
    handleSuccess();
  }
</script>
